package com.winning.sx.microframework.dao.sysmanage;

import com.winning.dapi.data.api.IDataAccessService;
import com.winning.dapi.data.entity.DataTable;
import com.winning.sx.microframework.model.exception.CustomException;
import com.winning.sx.microframework.model.manage.ModelManageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * Created by MJ on 2016-10-13.
 */
@Repository
public class SystemManageDao {
    @Autowired
    @Qualifier("dataSource")
    private IDataAccessService dataAccessService;

    /**
     * 获取系统信息列表
     *
     * @return
     * @throws CustomException
     */
    public DataTable querySystems() throws CustomException {
        DataTable table = null;
        try {
            String sql = "select cSystemID,iOrder,cSystemName,cVersion,cDescribe,cInputTime from pm_systems ORDER BY iOrder";
            table = dataAccessService.querySql(sql);
        } catch (Exception e) {
            throw new CustomException(-2001, "获取数据失败");
        }
        return table;
    }

    /**
     * 新增系统信息
     *
     * @param map
     * @throws CustomException
     */
    public int newSystem(Map map) throws CustomException {
        int result = 0;
        String sql = "insert into pm_systems(cSystemID,iOrder,cSystemName,cVersion,cDescribe,cInputTime) " +
                "values(:cSystemID,:iOrder,:cSystemName,:cVersion,:cDescribe,:cInputTime)";
        try {
            result = dataAccessService.executeSqlUsingParam(sql, map);
        } catch (Exception e) {
            throw new CustomException(-2002, "新增数据失败");
        }
        return result;
    }

    /**
     * 修改系统信息
     *
     * @param map
     * @throws CustomException
     */
    public int editSystem(Map map) throws CustomException {
        int result = 0;
        try {
            String sql = "update pm_systems set iOrder=:iOrder,cSystemName=:cSystemName,cVersion=:cVersion,cDescribe=:cDescribe,cInputTime=:cInputTime where cSystemID=:cSystemID";
            result = dataAccessService.executeSqlUsingParam(sql, map);
        } catch (Exception e) {
            throw new CustomException(-2002, "修改数据失败");
        }
        return result;
    }

    /**
     * 删除系统数据
     *
     * @param map
     * @throws CustomException
     */
    public int deleteSystem(Map map) throws CustomException {
        int result = 0;
        try {
            String sql = "delete from pm_systemasmodel where cSystemID=:cSystemID";
            result = this.dataAccessService.executeSqlUsingParam(sql, map);
            sql = "delete from pm_roleassystem where cSystemID=:cSystemID";
            result += this.dataAccessService.executeSqlUsingParam(sql, map);
            sql = "delete from pm_systems where cSystemID=:cSystemID";
            result += dataAccessService.executeSqlUsingParam(sql, map);
        } catch (Exception e) {
            throw new CustomException(-2003, "删除数据失败");
        }
        return result;
    }

    public int newAssociate(Map<String, Object> map) throws CustomException {
        int result = -1;
        try {
            String sql = "insert into pm_systemasmodel(cSystemID,cModelID) values(:systemid,:modelid)";
            result = dataAccessService.executeSqlUsingParam(sql, map);
//            sql="insert into pm_systemasmodel(cSystemID,cModelID) select :systemid,cFatherModelID from pm_models where cModelID=:modelid and cFatherModelID is not null and cFatherModelID not in (select cModelID from pm_systemasmodel where cSystemID=:systemid)";
//            result+=dataAccessService.executeSqlUsingParam(sql,map);
        } catch (Exception e) {
            throw new CustomException(-2002, "新增关联失败");
        }
        return result;
    }

    public int newChildAssociate(Map<String, Object> map) throws CustomException {
        int result = -1;
        try {
            String sql = "insert into pm_systemasmodel(cSystemID,cModelID) select :systemid,cModelID from pm_models where cFatherModelID=:modelid";
            result = dataAccessService.executeSqlUsingParam(sql, map);
        } catch (Exception e) {
            throw new CustomException(-2002, "新增关联失败");
        }
        return result;
    }

    public int deleteAssociate(Map<String, Object> map) throws CustomException {
        int result = -1;
        try {
            String sql = "delete from pm_systemasmodel where cSystemID=:systemid and cModelID=:modelid";
            result = dataAccessService.executeSqlUsingParam(sql, map);
//            sql="delete from pm_systemasmodel where cSystemID=:systemid and cModelID in (select cModelID from pm_models where cFatherModelID=:modelid)";
//            result+=dataAccessService.executeSqlUsingParam(sql,map);
        } catch (Exception e) {
            throw new CustomException(-2002, "删除关联失败");
        }
        return result;
    }

    public int deleteChildAssociate(Map<String, Object> map) throws CustomException {
        int result = -1;
        try {
            String sql = "delete from pm_systemasmodel where cSystemID=:systemid and cModelID in (select cModelID from pm_models where cFatherModelID=:modelid)";
            result += dataAccessService.executeSqlUsingParam(sql, map);
        } catch (Exception e) {
            throw new CustomException(-2002, "删除关联失败");
        }
        return result;
    }

    public List<ModelManageModel> queryModels(Map<String, Object> map) throws CustomException {
        List<ModelManageModel> list = null;
        try {
            String sql = "select cModelID,cModelName,cFatherModelID,cURL,cInputTime,case when cModelID in(select cModelID from pm_systemasmodel where cSystemID=:cSystemID) then 'true' else 'false' end as selected from pm_models order by cFatherModelID,iOrder";
            list = dataAccessService.querySqlUsingParamForBean(sql, ModelManageModel.class, map);
        } catch (Exception e) {
            throw new CustomException(-2001, "获取菜单失败");
        }
        return ModelManageDao.compositModels(list);
    }
}
